package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberMapper;
import com.itheima.health.dao.OrderMapper;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@DubboService
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * @Author: 15041
     * @Date: 19:45 2021/1/16
     * @Parms: []
     * @ReturnType: com.itheima.health.entity.Result
     * Description: 获取运营统计数据
     * map数据格式
     * reportDate -> yyyy-MM-dd        //日期
     * todayNewMember -> number		// 今日新增会员
     * thisWeekNewMember -> number		// 本周新增会员
     * thisMonthNewMember -> number	// 本月新增会员
     * totalMember -> number			//  总会员
     * todayOrderNumber -> number		// 今日预约人数
     * thisWeekOrderNumber -> number	// 本周预约人数
     * thisMonthOrderNumber -> number	// 本月预约人数
     * todayVisitsNumber -> number		// 今日到诊人数
     * thisWeekVisitsNumber -> number	// 本周到诊人数
     * thisMonthVisitsNumber -> number	// 本月到诊人数
     * hotSetmeal ->  [ {"proportion":0.4545,"name":"粉红珍爱(女)升级TM12项筛查体检套餐","setmeal_count":5}]	// 热门套餐
     */
    @Override
    public Map<String, Object> getBussinessReport() {
        Map<String, Object> resultMap = new HashMap<>();

        //日期处理
        //获取当前日期
        Calendar today = Calendar.getInstance();
        //将时分秒毫秒清零
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);

        //设置周一为每周第一天  因为国外都是周日为每周第一天
        today.setFirstDayOfWeek(Calendar.MONDAY);

        //创建本周第一天
        Calendar weekFirstDay = (Calendar) today.clone();
        //设置本周第一天
        weekFirstDay.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        //创建本周最后一天
        Calendar weekLastDay = (Calendar) today.clone();
        //设置本周最后一天
        weekLastDay.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

        //创建本月第一天
        Calendar monthFirstDay = (Calendar) today.clone();
        //设置本月第一天
        monthFirstDay.set(Calendar.DAY_OF_MONTH, 1);

        //创建本月最后一天
        Calendar monthLastDay = (Calendar) today.clone();
        //获取本月最后一天
        int maxDayOfMonth = monthLastDay.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置本月最后一天
        monthLastDay.set(Calendar.DAY_OF_MONTH, maxDayOfMonth);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        //获取会员数据
        //设置日期
        resultMap.put("reportDate",df.format(today.getTime()));
        //今日新增会员 查询条件 今天 - 今天
        resultMap.put("todayNewMember",memberMapper.coutByRegTimeRange(today.getTime(),today.getTime()));
        //本周新增会员 查询条件 本周第一天 - 本周最后一天
        resultMap.put("thisWeekNewMember",memberMapper.coutByRegTimeRange(weekFirstDay.getTime(),weekLastDay.getTime()));
        //本月新增会员 查询条件 本月第一天 - 本月最后一天
        resultMap.put("thisMonthNewMember",memberMapper.coutByRegTimeRange(monthFirstDay.getTime(),monthLastDay.getTime()));
        //总会员数
        resultMap.put("totalMember",memberMapper.coutByRegTimeBeRange(df.format(today.getTime())));

        //预约数据 orderMapper
        //今日预约人数 查询条件 今天 - 今天
        resultMap.put("todayOrderNumber",orderMapper.countByOrderDateRange(today.getTime(),today.getTime(),null));
        //本周预约人数 查询条件 本周第一天 - 本周最后一天
        resultMap.put("thisWeekOrderNumber",orderMapper.countByOrderDateRange(weekFirstDay.getTime(),weekLastDay.getTime(),null));
        //本月预约人数 查询条件 本月第一天 - 本月最后一天
        resultMap.put("thisMonthOrderNumber",orderMapper.countByOrderDateRange(monthFirstDay.getTime(),monthLastDay.getTime(),null));

        //今日到诊人数 查询条件 今天 - 今天 - 已到诊状态
        resultMap.put("todayVisitsNumber",orderMapper.countByOrderDateRange(today.getTime(),today.getTime(),Order.ORDERSTATUS_YES));
        //本周到诊人数 查询条件 本周第一天 - 本周最后一天 - 已到诊状态
        resultMap.put("thisWeekVisitsNumber",orderMapper.countByOrderDateRange(weekFirstDay.getTime(),weekLastDay.getTime(),Order.ORDERSTATUS_YES));
        //本月到诊人数 查询条件 本月第一天 - 本月最后一天 - 已到诊状态
        resultMap.put("thisMonthVisitsNumber",orderMapper.countByOrderDateRange(monthFirstDay.getTime(),monthLastDay.getTime(),Order.ORDERSTATUS_YES));

        //热门套餐 查询条件 可以存放的个数 4
        resultMap.put("hotSetmeal",orderMapper.countByHotSetmeal(4));

        return resultMap;
    }
}
